package sqls

import (
	"context"
	"errors"
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/auths"
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/enums/sys_enum"
	"github.com/jackc/pgtype"
)

func FillAuthMap(ctx context.Context, authMap map[string]interface{}) error {
	user := auths.GetUser(ctx)
	if user == nil {
		return errors.New("用户信息不存在")
	}
	authMap["auths_DataAccess"] = user.DataAccess
	authMap["auths_ProvinceId"] = user.ProvinceId
	authMap["auths_CityId"] = user.CityId
	authMap["auths_AreaId"] = user.AreaId
	authMap["auths_OrganizationIds"] = user.OrganizationIds
	authMap["auths_DepartmentIds"] = user.DepartmentIds
	authMap["auths_DepartmentId"] = user.DepartmentId
	authMap["auths_OrganizationId"] = user.OrganizationId
	authMap["auths_UserId"] = user.UserId

	authsOrganizationIds := pgtype.TextArray{}
	authsOrganizationIds.Set(user.OrganizationIds)
	authMap["auths_OrganizationIds_array"] = &authsOrganizationIds

	/*if v, ok := authMap["auths_DataAccess"]; ok {
		switch v2 := v.(type) {
		case json.Number:
			i, err := v2.Int64()
			if err != nil {
				return err
			}
			authMap["auths_DataAccess"] = int(i)
		}
		authMap["auths_ProvinceId"] = user.ProvinceId
		authMap["auths_CityId"] = user.CityId
		authMap["auths_AreaId"] = user.AreaId
		authMap["auths_OrganizationIds"] = user.OrganizationIds
		authMap["auths_DepartmentIds"] = user.DepartmentIds
		authMap["auths_DepartmentId"] = user.DepartmentId
		authMap["auths_OrganizationId"] = user.OrganizationId
		authMap["auths_UserId"] = user.UserId
	} else {
		authMap["auths_DataAccess"] = user.DataAccess
		switch user.DataAccess {
		case sys_enum.SysUserGrade.DataAccessProvince():
			authMap["auths_ProvinceId"] = user.ProvinceId
		case sys_enum.SysUserGrade.DataAccessCity():
			authMap["auths_CityId"] = user.CityId
		case sys_enum.SysUserGrade.DataAccessArea():
			authMap["auths_AreaId"] = user.AreaId
		case sys_enum.SysUserGrade.DataAccessOrg():
			authMap["auths_OrganizationIds"] = user.OrganizationIds
		case sys_enum.SysUserGrade.DataAccessDept():
			authMap["auths_DepartmentIds"] = user.DepartmentIds
		case sys_enum.SysUserGrade.DataAccessUser():
			authMap["auths_UserId"] = user.UserId

		}
	}*/

	return nil

}

func FillTestAuthMap(ctx context.Context, authMap map[string]interface{}) error {
	authMap["auths_DataAccess"] = sys_enum.DATA_ACCESS_ALL
	authMap["auths_ProvinceId"] = "1"
	authMap["auths_CityId"] = "2"
	authMap["auths_AreaId"] = "3"
	authMap["auths_OrganizationIds"] = []string{"1", "2"}
	authMap["auths_DepartmentIds"] = []string{"1", "2"}
	authMap["auths_DepartmentId"] = "1"
	authMap["auths_OrganizationId"] = "2"
	authMap["auths_UserId"] = "3"
	authsOrganizationIds := pgtype.TextArray{}
	authsOrganizationIds.Set([]string{"1", "2"})
	authMap["auths_OrganizationIds_array"] = &authsOrganizationIds
	return nil
}

func FillUserMap(ctx context.Context, authMap map[string]interface{}) error {
	user := auths.GetUser(ctx)
	authMap["user_UserId"] = user.UserId
	authMap["user_DepartmentId"] = user.DepartmentId
	authMap["user_OrganizationId"] = user.OrganizationId
	authMap["user_ProvinceId"] = user.ProvinceId
	authMap["user_CityId"] = user.CityId
	authMap["user_DistrictId"] = user.AreaId
	authMap["user_RoleId"] = user.RoleId
	authMap["user_AgentIds"] = user.AgentIds
	authMap["user_SateliteOperatorIds"] = user.SateliteOperatorIds

	return nil
}
